/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddDownloadView.java
 *
 * Created on 2011-05-31, 12:44:16
 */

package ytmc.downloader;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;
import ytmc.DialogView;

/**
 *
 * @author K
 */
public class AddDownloadTaskView extends DialogView
{
    private DownloaderController controller;

    public String getSavePath()
    {
        return this.videoSavePathField.getText();
    }

    public void setSavePath(String path)
    {
        this.videoSavePathField.setText(path);
    }

    public String getDownloadURL()
    {
        return this.videoURLField.getText();
    }

    public void setDownloadURL(String url)
    {
        this.videoURLField.setText(url);
    }

    /** Creates new form AddDownloadView */
    public AddDownloadTaskView(DownloaderController controller)
    {
        this.controller = controller;
        
        initComponents();
        setLocationRelativeTo(null);

        DocumentListenerImpl listener = new DocumentListenerImpl();
        videoSavePathField.getDocument().addDocumentListener(listener);
        videoURLField.getDocument().addDocumentListener(listener);
    }

    public void clear()
    {
        videoSavePathField.setText("");
        videoURLField.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        videoSavePathField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        videoURLField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        showFileDialogBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Dodaj nowe zadanie pobierania");
        setResizable(false);

        addBtn.setText("Dodaj");
        addBtn.setEnabled(false);
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });

        cancelBtn.setText("Anuluj");
        cancelBtn.setDefaultCapable(false);
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });

        jLabel2.setText("Zapisz jako");

        jLabel1.setText("Link do wideo");

        showFileDialogBtn.setText("...");
        showFileDialogBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showFileDialogBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(videoURLField, javax.swing.GroupLayout.DEFAULT_SIZE, 523, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(51, 51, 51))
                            .addComponent(videoSavePathField, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(showFileDialogBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(addBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelBtn)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(videoURLField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(videoSavePathField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(showFileDialogBtn))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelBtn)
                    .addComponent(addBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addBtnActionPerformed
    {//GEN-HEADEREND:event_addBtnActionPerformed
        controller.addTask();
        this.setVisible(false);
}//GEN-LAST:event_addBtnActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelBtnActionPerformed
    {//GEN-HEADEREND:event_cancelBtnActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_cancelBtnActionPerformed

    private void showFileDialogBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showFileDialogBtnActionPerformed
    {//GEN-HEADEREND:event_showFileDialogBtnActionPerformed
        JFileChooser saveFileDialog = new JFileChooser();
        saveFileDialog.setAcceptAllFileFilterUsed(false);
        saveFileDialog.setCurrentDirectory(FileSystemView.getFileSystemView().getHomeDirectory());
        saveFileDialog.setDialogType(JFileChooser.SAVE_DIALOG);
        saveFileDialog.setFileFilter(new FileFilterImpl());
        saveFileDialog.setDialogTitle("Zapisz plik jako:");

        int result = saveFileDialog.showSaveDialog(this);
        if(result == JFileChooser.APPROVE_OPTION)
        {
            String path = saveFileDialog.getSelectedFile().getAbsolutePath();
            if(!path.endsWith(".flv"))
            {
                path += ".flv";
            }

            this.setSavePath(path);
        }
    }//GEN-LAST:event_showFileDialogBtnActionPerformed

    private class FileFilterImpl extends FileFilter
    {
        @Override
        public boolean accept(File file)
        {
            if(file.isDirectory())
            {
                return true;
            }

            String name = file.getName().toLowerCase();
            if(name.endsWith(".flv"))
            {
                return true;           
            }

            return false;
        }

        @Override
        public String getDescription()
        {
            return "Pliki flash wideo (*.flv)";
        }
    }

    private class DocumentListenerImpl implements DocumentListener
    {
        @Override
        public void insertUpdate(DocumentEvent e)
        {
            if(!videoSavePathField.getText().isEmpty() && !videoURLField.getText().isEmpty())
            {
                addBtn.setEnabled(true);
            }
        }

        @Override
        public void removeUpdate(DocumentEvent e)
        {
            if(videoSavePathField.getText().isEmpty() || videoURLField.getText().isEmpty())
            {
                addBtn.setEnabled(false);
            }
        }

        @Override
        public void changedUpdate(DocumentEvent e)
        {
            if(!videoSavePathField.getText().isEmpty() && !videoURLField.getText().isEmpty())
            {
                addBtn.setEnabled(true);
            }
            else
            {
                addBtn.setEnabled(false);
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton showFileDialogBtn;
    private javax.swing.JTextField videoSavePathField;
    private javax.swing.JTextField videoURLField;
    // End of variables declaration//GEN-END:variables

}
